#include "stm32f10x_conf.h"
#include "timer.h"
#include <stdio.h>
#include "uart.h"
#include <string.h>
#include "network.h"
#include "iap_pro.h"

//#define NO_BOOTLOADER

#define BUFFER_SIZE 256

static uint8_t s_ucUart1Buffer[BUFFER_SIZE];

//用于实现打印printf函数
int fputc(int ch, FILE *f)
{
    while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
    USART_SendData(USART1, (unsigned char)ch);
    while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
    return ch;
}

//初始化打印调试串口
void uart1_init(void)
{
	UartInit(UART_1,115200,s_ucUart1Buffer,BUFFER_SIZE,50,0);
}

int main()
{
	#ifndef NO_BOOTLOADER
    NVIC_SetVectorTable(NVIC_VectTab_FLASH,FLASH_APP1_ADDRESS - FLASH_BOOT_ADDRESS);
    #endif
    SysTick_Config(SystemCoreClock/1000);
	SetUartTimeStamp(GetTimeTick);
	uart1_init();
	NetworkInit();
	uint16_t len = 0;
	while(1)
	{
        len = UartRead(UART_1,false,0,BUFFER_SIZE);
        if(len)
        {
              if(strstr((const char*)s_ucUart1Buffer,"network=on"))
			  {
				  NetworkStart();
				  printf("Network Start!\r\n");
			  }
			  else if(strstr((const char*)s_ucUart1Buffer,"network=off"))
			  {
				  NetworkStop();
				  printf("Network Stop!\r\n");
			  }
			  else if(strstr((const char*)s_ucUart1Buffer,"p=report"))
			  {
				  NetworkReport();
				  printf("report Stop!\r\n");
			  }
			  else if(strstr((const char*)s_ucUart1Buffer,"ota"))
			  {
				  NetworkRequsetOTA();
				  printf("ota request!\r\n");
			  }
        }
		NetworkLoop();
	}
}

